package com.chandra.pos.product.service.impl;

import com.chandra.pos.product.dao.entity.ProductEntity;
import com.chandra.pos.product.dao.entity.ProductRepository;
import com.chandra.pos.product.model.ProductFilter;
import com.chandra.pos.product.model.ProductList;
import com.chandra.pos.product.model.ProductModel;
import com.chandra.pos.product.service.ProductRole;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 8/15/14.
 */
@Component
public class ProductRoleImpl implements ProductRole {

    @Autowired
    ProductRepository productRepository;

    public ProductModel saveProduct(ProductModel productModel) throws EntityExistsException {

        ProductEntity productEntity = null;
        if (productModel.getProductId() != null) {

            productEntity = productRepository.findOne(productModel.getProductId());

            if (!productEntity.getName().equalsIgnoreCase(productModel.getName())) {

                ProductEntity productEntity1 = productRepository.findByName(productModel.getName());
                if (productEntity1 != null)
                    throw new EntityExistsException("dsds");

            }

            BeanUtils.copyProperties(productModel, productEntity);


        } else {

            productEntity = new ProductEntity();

            ProductEntity productEntity1 = productRepository.findByName(productModel.getName());
            if (productEntity1 != null)
                throw new EntityExistsException("dsds");

        }


        BeanUtils.copyProperties(productModel, productEntity);
        productEntity = productRepository.save(productEntity);

        productModel.setProductId(productEntity.getProductId());
        return productModel;
    }

    @Override
    public ProductList search(ProductFilter productFilter) {

        ProductList productList = new ProductList();

        List<ProductModel> productModels = new ArrayList<>();
        Page<ProductEntity> productEntity1 = null;

        PageRequest request =
                new PageRequest(productFilter.getPage() - 1, productFilter.getRows(), productFilter.getDBSord(), productFilter.getSidx());

        if (!StringUtils.isEmpty(productFilter.getQuery()))

            productEntity1 = productRepository.findByNameContainingIgnoreCase(productFilter.getQuery().toUpperCase() , request);
        else
            productEntity1 = productRepository.findAll(request);


        for (ProductEntity productEntity : productEntity1.getContent()) {
            ProductModel productModel = new ProductModel();

            BeanUtils.copyProperties(productEntity, productModel);
            ;
            productModels.add(productModel);
        }
        productList.setPage(productFilter.getPage());
        productList.setRecords(productEntity1.getTotalElements());
        productList.setTotal(productEntity1.getTotalPages());

        productList.setProducts(productModels);
        return productList;
    }

    @Override
    public ProductModel getProductDetail(ProductFilter productFilter) throws EntityNotFoundException {


        ProductEntity productEntity = null;

        if (productFilter.getId() != null)
            productEntity = productRepository.findOne(productFilter.getId());

        else if (StringUtils.hasText(productFilter.getName()))

            productEntity = productRepository.findByName(productFilter.getName());

       else  if (StringUtils.hasText(productFilter.getSku()))

            productEntity = productRepository.findBySku(productFilter.getSku());

        if (productEntity == null)
            throw new EntityNotFoundException("Product with name " + productFilter.getId() == null ? productFilter.getQuery() : productFilter.getId() + " not found");


        ProductModel productModel = new ProductModel();

        BeanUtils.copyProperties(productEntity, productModel);

        return productModel;


    }
}
